// Project: usb ondokei01
// FileName: main.c


#include "./USB/usb.h"
#include "./USB/usb_function_cdc.h"
#include "HardwareProfile.h"
#include "GenericTypeDefs.h"
#include "Compiler.h"
#include "usb_config.h"
#include "USB\usb_device.h"
#include "USB\usb.h"

#include <delays.h>
#include "lib_adc.h"
#include <timers.h>

/** CONFIGURATION **************************************************/

#pragma config CPUDIV = NOCLKDIV
#pragma config USBDIV = OFF
#pragma config FOSC = HS
#pragma config PLLEN = ON
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRTEN = OFF
#pragma config BOREN = OFF
#pragma config BORV = 30
#pragma config WDTEN = OFF
#pragma config WDTPS = 32768
#pragma config MCLRE = OFF
#pragma config HFOFST = OFF
#pragma config STVREN = ON
#pragma config LVP = OFF
#pragma config XINST = OFF
#pragma config BBSIZ = OFF
#pragma config CP0 = OFF
#pragma config CP1 = OFF
#pragma config CPB = OFF
#pragma config WRT0 = OFF
#pragma config WRT1 = OFF
#pragma config WRTB = OFF
#pragma config WRTC = OFF
#pragma config EBTR0 = OFF
#pragma config EBTR1 = OFF
#pragma config EBTRB = OFF

/** V A R I A B L E S ********************************************************/
#pragma udata
char USB_In_Buffer[64];
char USB_Out_Buffer[64];


#define lcd_DB LATB
#define lcd_E LATCbits.LATC3
#define lcd_RS LATCbits.LATC4

#define Delay_50uS Delay100TCYx(6)
#define Delay_1mS Delay1KTCYx(12)
#define Delay_2mS Delay1KTCYx(24)
#define Delay_5mS Delay10KTCYx(6)
#define Delay_10mS Delay10KTCYx(12)
#define Delay_20mS Delay10KTCYx(24)
#define Delay_100mS Delay10KTCYx(120)

/** P R I V A T E P R O T O T Y P E S ***************************************/
static void InitializeSystem(void);
void ProcessIO(void);
void USBDeviceTasks(void);
void YourHighPriorityISRCode();
void YourLowPriorityISRCode();
void UserInit(void);

void lcd_w4(char asci);
void lcd_c4(char cmd);
void lcd_clr(void);
void lcd_init(void);
void lcd_puts(char *s);
void lcd_locate(char y,char x);

/** VECTOR REMAPPING ***********************************************/

#define REMAPPED_RESET_VECTOR_ADDRESS 0x00
#define REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS 0x08
#define REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS 0x18

#pragma code REMAPPED_HIGH_INTERRUPT_VECTOR = REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS
void Remapped_High_ISR (void)
{
_asm goto YourHighPriorityISRCode _endasm
}
#pragma code REMAPPED_LOW_INTERRUPT_VECTOR = REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS
void Remapped_Low_ISR (void)
{
_asm goto YourLowPriorityISRCode _endasm
}

#pragma code

#pragma interrupt YourHighPriorityISRCode
void YourHighPriorityISRCode()
{
USBDeviceTasks(); //USB_POLLING
}
#pragma interruptlow YourLowPriorityISRCode
void YourLowPriorityISRCode()
{

}

#pragma code

void lcd_w4(char c){
lcd_DB = (c & 0xF0) | (lcd_DB & 0x0F);
lcd_E = 1;
Delay10TCYx(20);
lcd_E = 0;
}

void lcd_c4(char c){
lcd_RS = 0;
lcd_w4(c);
lcd_w4(c<<4);
if(c & 0xFC)
Delay_50uS;
else
Delay_2mS;
}

void lcd_clr(void){
lcd_c4(0x01);
}

void lcd_locate(char y, char x){
unsigned char p;
switch(y & 0x03){
case 0: p=0x80; break;
case 1: p=0xC0; break;
case 2: p=0x94; break;
case 3: p=0xD4; break;
}
lcd_c4(p += x);
}

void lcd_putchr(char c){
lcd_RS = 1;
lcd_w4(c);
lcd_w4(c<<4);
Delay_50uS;
}

void lcd_puts(char *s){
while(*s)
lcd_putchr(*s++);
}

void lcd_putv(int v){
char i;
char buf[5];
v *=49;
for(i=0;i<5;i++)
buf[i]='0';
i=4;
do{
buf[i]=(v%10)+'0';
v=v/10;
i--;
} while(v>0);
lcd_putchr(buf[0]);
lcd_putchr('.');
for(i=1;i<4;i++)
lcd_putchr(buf[i]);
}

void lcd_putt(int t){
char i;
char buf[5];
t *=10;
for(i=0;i<5;i++)
buf[i]='0';
i=4;
do{
buf[i]=(t%10)+'0';
t=t/10;
i--;
} while(t>0);
lcd_putchr(buf[1]);
lcd_putchr(buf[2]);
lcd_putchr('.');
lcd_putchr(buf[3]);
}


void lcd_init(void){
Delay_20mS;
lcd_RS = 0;
lcd_w4(0x30);Delay_5mS;
lcd_w4(0x30);Delay_1mS;
lcd_w4(0x30);Delay_1mS;
lcd_w4(0x20);Delay_1mS;
lcd_c4(0x2E);
lcd_c4(0x08);
lcd_c4(0x0C);
lcd_c4(0x06);
lcd_c4(0x01);
}

char unit[] = "(V)";
char ch1[] = "ch1 ";
char ch2[] = "ch2 ";
char temp[] = "temp ";
char unitc[] = "(C)";
char blank[] = " ";

int v,v0,v1, t,t0;
unsigned char sw;



void main(void){

InitializeSystem();

PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
TRISA = 0b00000000;
TRISB = 0b00000000;
TRISC = 0b10000001;
ANSEL = 0b00000000;
ANSELH = 0b00000010;

lcd_init();

if(USB_BUS_SENSE && (USBGetDeviceState() == DETACHED_STATE)){
USBDeviceAttach();
}
USBDeviceTasks();


while(1){

int i, j;
for(i=0; i<10; i++){
for(j=0; j<479; j++){
Delay_1mS;
}

OpenADC(ADC_FOSC_16 &
ADC_RIGHT_JUST &
ADC_12_TAD,
ADC_REF_VDD_VREFPLUS &
ADC_REF_VDD_VSS,
ADC_CH9 &
ADC_INT_OFF );
ConvertADC();
while(BusyADC());
v=(ADRESH*256) + ADRESL;

v1=v;
lcd_locate(0,2);
lcd_puts(ch1);
lcd_putt(v1);
lcd_puts(unitc);
}
ProcessIO();
}
}


static void InitializeSystem(void)
{
ADCON0 = 0x00;


UserInit();

USBDeviceInit();
}


void UserInit(void)
{


//Initialize all of the LED pins
mInitAllLEDs();

//Initialize the pushbuttons
mInitAllSwitches();

sw = 0;
}


void ProcessIO(void){

char i;
char buf[6];
BYTE numBytesRead;

numBytesRead = getsUSBUSART(USB_Out_Buffer,64);
if(numBytesRead != 0){
BYTE i;

for(i=0;i<numBytesRead;i++){
switch(USB_Out_Buffer[i]){
case 0x0A:
if(sw == 0){
sw = 1;
}else{
sw = 0;
}
break;
default:
break;
}
}
}

if(sw == 1){
if(mUSBUSARTIsTxTrfReady()){

v *=10;
for(i=0;i<5;i++)
buf[i]='0';
i=4;
do{
buf[i]=(v%10)+'0';
v=v/10;
i--;
} while(v>0);

buf[0]=buf[1]; buf[1]=buf[2]; buf[3]=buf[3];
buf[2]='.'; buf[4]='\n';

for(i=0;i<5;i++){
USB_In_Buffer[i] = buf[i];
}
putUSBUSART(USB_In_Buffer,5);
}
}
CDCTxService();
}



// (ここから下は変更無し)



void USBCBSuspend(void)
{
#if defined(__C30__)
#if 0
U1EIR = 0xFFFF;
U1IR = 0xFFFF;
U1OTGIR = 0xFFFF;
IFS5bits.USB1IF = 0;
IEC5bits.USB1IE = 1;
U1OTGIEbits.ACTVIE = 1;
U1OTGIRbits.ACTVIF = 1;
Sleep();
#endif
#endif
}

#if 0
void __attribute__ ((interrupt)) _USB1Interrupt(void)
{
#if !defined(self_powered)
if(U1OTGIRbits.ACTVIF) {
IEC5bits.USB1IE = 0;
U1OTGIEbits.ACTVIE = 0;
IFS5bits.USB1IF = 0;

//USBClearInterruptFlag(USBActivityIFReg,USBActivityIFBitNum);
USBClearInterruptFlag(USBIdleIFReg,USBIdleIFBitNum);
//USBSuspendControl = 0;
}
#endif
}
#endif

void USBCBWakeFromSuspend(void)
{

}

void USBCB_SOF_Handler(void)
{

}

void USBCBErrorHandler(void)
{

}

void USBCBCheckOtherReq(void)
{
USBCheckCDCRequest();
}

void USBCBStdSetDscHandler(void)
{

}

void USBCBInitEP(void)
{
CDCInitEP();
}

void USBCBSendResume(void)
{
static WORD delay_count;

USBResumeControl = 1; // Start RESUME signaling

delay_count = 1800U; // Set RESUME line for 1-13 ms
do {
delay_count--;
} while(delay_count);
USBResumeControl = 0;
}

#if defined(ENABLE_EP0_DATA_RECEIVED_CALLBACK)
void USBCBEP0DataReceived(void)
{

}
#endif

BOOL USER_USB_CALLBACK_EVENT_HANDLER(USB_EVENT event, void *pdata, WORD size)
{
switch(event) {
case EVENT_CONFIGURED:
USBCBInitEP();
break;
case EVENT_SET_DESCRIPTOR:
USBCBStdSetDscHandler();
break;
case EVENT_EP0_REQUEST:
USBCBCheckOtherReq();
break;
case EVENT_SOF:
USBCB_SOF_Handler();
break;
case EVENT_SUSPEND:
USBCBSuspend();
break;
case EVENT_RESUME:
USBCBWakeFromSuspend();
break;
case EVENT_BUS_ERROR:
USBCBErrorHandler();
break;
case EVENT_TRANSFER:
Nop();
break;
default:
break;
}
return TRUE;
}







                戻る